package org.net.cms.service.notice;

import jakarta.annotation.Resource;
import org.net.cms.controller.admin.notice.vo.NoticeReqVO;
import org.net.cms.dao.NoticeDto;
import org.net.cms.enums.NoticeEnums;
import org.net.cms.mapper.NoticeMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public List<NoticeDto> listNotice(int type) {
        List<NoticeDto> list = new ArrayList<>();

        if (NoticeEnums.LIST_NOTICE.getStatus().equals(type)) {
            // 查看所有的公告
            list = noticeMapper.listAllNotice();
        } else {
            // 查看已/未发布列表，0未发布，1已发布
            list = noticeMapper.listPublishNotice(type);
        }
        return list;
    }

    @Override
    public NoticeDto getNotice(int id) {
        NoticeDto noticeById = noticeMapper.getNoticeById(id);
        if (noticeById.getDeleted() == 1) {
            return null;
        }
        return noticeById;
    }

    @Override
    public int addNotice(NoticeReqVO notice) {
        if (notice.getDate() == null) {
            notice.setDate(LocalDate.now());
        }
        noticeMapper.addNotice(notice);
        return 0;
    }

    @Override
    public int updateNotice(NoticeReqVO notice) {
        if (notice.getDate() == null) {
            notice.setDate(LocalDate.now());
        }
        noticeMapper.updateById(notice);
        return 0;
    }

    @Override
    public boolean deleteNotice(int id) {
        int i = noticeMapper.deleteById(id);
        System.out.println(i);
        return Boolean.TRUE;
    }
}
